<?php

class Official_medal_order_model extends CI_Model {

    function OfficialMedalOrderModel() {
        parent::Model();
    }

    function add() {
        $query = $this->db->get_where('Official_Medal_Order', array('Medal_ID' => check_post('medal'), 'Official_Order_ID' => check_post('official_order_ID')));
        if (!$query->num_rows) {
            $this->db->insert('Official_Medal_Order', array(
                'Official_Order_ID' => check_post('official_order_ID'),
                'Medal_ID' => check_post('medal'),
                'amount' => check_post('amount'),
                'possible_amount' => check_post('remain-amount')
            ));
            if ($this->db->affected_rows() > 0) {
                $return['result'] = true;
                $return['message'] = 'បន្ថែមចំនួនមេដាយត្រូវការ បានជោគជ័យ.';
            } else {
                $return['result'] = false;
                $return['message'] = 'មិនអាចរក្សារទុកទិន្នន័យនេះបាន សូមព្យាយាមម្តងទៀត.';
            }
        } else {
            $return['result'] = false;
            $return['message'] = 'មេដាយនេះត្រូវបានជ្រើសរើសរួចម្តង ក្នុងក្រឹតនេះ.';
        }
        return $return;
    }

    function get_official_medal_order() {

        $aColumns = array('Official_Medal_Order.ID', 'Medal_Type.name', 'Medal_Class.name', 'Official_Medal_Order.possible_amount', 'Official_Medal_Order.amount');

        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Official_Medal_Order.ID", "ASC");
        }
        $order = $this->db->ar_orderby;

        $where = " Official_Medal_Order.Official_Order_ID = '" . check_get('official_order_ID') . "' ";

        $this->db->where($where);

        $this->db->select('Official_Medal_Order.ID');
        $this->db->join('Medal', 'Medal.ID= Official_Medal_Order.Medal_ID', 'inner');
        $this->db->join('Medal_Type', 'Medal.Medal_Type_ID= Medal_Type.ID', 'inner');
        $this->db->join('Medal_Class', 'Medal.Medal_Class_ID= Medal_Class.ID', 'inner');
        $totalQuery = $this->db->get('Official_Medal_Order');

        $this->db->where($where);
        $this->db->select('Official_Medal_Order.ID, Medal_Type.name type_name, Medal_Class.name class_name, possible_amount, amount');
        $this->db->join('Medal', 'Medal.ID= Official_Medal_Order.Medal_ID', 'inner');
        $this->db->join('Medal_Type', 'Medal.Medal_Type_ID= Medal_Type.ID', 'left');
        $this->db->join('Medal_Class', 'Medal.Medal_Class_ID= Medal_Class.ID', 'left');
        $query = $this->db->get('Official_Medal_Order', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $official_medal_orders = $query->result_array();
        $aaData = array();
        $i = 0;
        foreach ($official_medal_orders as $official_medal_order) {
            $data = array(++$i, $official_medal_order['type_name'], $official_medal_order['class_name'], $official_medal_order['amount'], $official_medal_order['possible_amount'], "<a class='delete-btn delete-official-medal' rel='{$official_medal_order['ID']}' title='លុប'>&nbsp;</a>");
            array_push($aaData, $data);
        }


        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

    function deleteOfficialMedalOrder() {
        $this->db->delete('Official_Medal_Order', array('ID' => check_post("ID")));
    }

}

?>